package com.dgut.service.serviceImpl;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dgut.common.*;
import com.dgut.mapper.OrderFlagMapper;
import com.dgut.mapper.QualificationCertificateMapper;
import com.dgut.mapper.UserMapper;
import com.dgut.pojo.OrderFlag;
import com.dgut.pojo.QualificationCertificate;
import com.dgut.pojo.ServiceProvider;
import com.dgut.pojo.User;
import com.dgut.service.NoticeService;
import com.dgut.service.QualificationCertificateService;
import com.dgut.utils.PageRequestInfo;
import com.dgut.utils.PageResult;
import com.dgut.utils.PageUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

@SuppressWarnings({"all"})
@Service
public class QualificationCertificateServiceImpl implements QualificationCertificateService {
    @Resource
    private QualificationCertificateMapper qualificationCertificateMapper;
    @Resource
    private OrderFlagMapper orderFlagMapper;
    @Resource
    private NoticeService noticeService;

    /**
     * start-zwb
     */
    //通过id精准查询资质证照,返回对象
    public Object selectQualificationCertificateByIdForOrder(int id)
    {
        return qualificationCertificateMapper.selectById(id);
    }
    /**
     * end-zwb
     */


    /**
     * start-gxd
     */

    @Override
    public Object getQualificationCertificateByPage(PageRequestInfo pageRequestInfo){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Page<QualificationCertificate> page = new Page(pageRequestInfo.getPageNum(), pageRequestInfo.getPageSize());
        Page<QualificationCertificate> resultPage = qualificationCertificateMapper
                .selectPage(page,null);
        PageResult pageResult = PageUtils.getPageResult(resultPage);
        if (pageResult != null){
            resultMap.put("pageResult",pageResult);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    public Object selectQualificationCertificateListByName(String name){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QueryWrapper<QualificationCertificate> wrapper = new QueryWrapper<>();
        if (name == null){
            List<QualificationCertificate> list = qualificationCertificateMapper.selectList(null);
            if(list != null){
                resultMap.put("list",list);
                resultMap.put("result",true);
                resultMap.put("message","查询成功！");
            }   else {
                resultMap.put("result", false);
                resultMap.put("message", "查询失败！");
            }
        }
        else {
            wrapper.like("name", name);
            List<QualificationCertificate> list = qualificationCertificateMapper.selectList(wrapper);
            if(list != null){
                resultMap.put("list",list);
                resultMap.put("result",true);
                resultMap.put("message","查询成功！");
            }   else {
                resultMap.put("result", false);
                resultMap.put("message", "查询失败！");
            }
        }
        return resultMap;
    }

    @Override
    public Object selectQualificationCertificateById(int id){
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        QualificationCertificate qualificationCertificate = qualificationCertificateMapper.selectById(id);
        if (qualificationCertificate != null){
            resultMap.put("qualificationCertificate",qualificationCertificate);
            resultMap.put("result",true);
            resultMap.put("message","查询成功！");
        } else {
            resultMap.put("result",false);
            resultMap.put("message","查询失败！");
        }
        return resultMap;
    }

    @Override
    @SaCheckLogin//登陆验证
    @SaCheckRole(SelectRole.BUYER)//角色认证
    public Object buyQualificationCertificate(QualificationCertificate qualificationCertificate){
        //1.获取采买者的id
        ConcurrentHashMap resultMap = new ConcurrentHashMap();
        Object loginId = StpUtil.getLoginId();
        int buyerId = Integer.parseInt(loginId.toString());
        //2.生成订单
        OrderFlag orderFlag = new OrderFlag(buyerId,null, OrderType.QUALIFICATION_CERTIFICATE,
                qualificationCertificate.getId(),new Date(),null,new Date(),
                qualificationCertificate.getMoney(), StateForOrder.WAITING_FOR_ASSIGNED,
                OnOrOff.ON, ExceptionIdentificationForOrder.NORMAL);
        //3.插入数据库
        int update = orderFlagMapper.insert(orderFlag);
        noticeService.orderChangedStateNotice(orderFlag);
        if (update>=1){
            resultMap.put("result",true);
            resultMap.put("message","订单已生成！");
        }
        else {
            resultMap.put("result",false);
            resultMap.put("message","订单生成失败！");
        }
        return resultMap;
    }

    /**
     * end-gxd
     */


    /**
     * start-lzh
     */
    /**
     * end-lzh
     */

    /**
     * start-hwf
     */
    /**
     * end-hwf
     */
    /**
     * start-hqq
     */
    //通过名称分页查询上线的资质证照
    @Override
    public Object getQualificationCertificateByName(PageRequestInfo pageRequestInfo,String name) {
        QueryWrapper<QualificationCertificate> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state","上线");
        if(name!=null){
            queryWrapper.like("name",name);
        }
        Page<QualificationCertificate> page = new Page<>(pageRequestInfo.getPageNum(),pageRequestInfo.getPageSize());
        Page<QualificationCertificate> resultPage = qualificationCertificateMapper.selectPage(page,queryWrapper);
        PageResult result = PageUtils.getPageResult(resultPage);
        ConcurrentHashMap map = new ConcurrentHashMap();
        map.put("qualityServiceProvider",result);
        return map;
    }
    /**
     * end-hqq
     */
}
